//获取工具类的应用实例  
const util = require('../../../utils/util.js');
const api = require('../../../utils/api.js');
const App = getApp();

Page({
  data: {

    count: 0,
    //页面适配
    imgwidth: 0,
    imgheight: 0,
    windowWidth: wx.getSystemInfoSync().screenWidth,
    windowHeight: wx.getSystemInfoSync().windowHeight,
    //wxml渲染层参数
    goods_data: {
      list: [],
      page: {
        page_no: 0,
        page_size: 6,
      },
      paginate: {}
    },
    //返回顶部
    scrollTop: 0,
    goTop_show: false,
    //加载完显示
    loade: true,
  },

  //初始化数据加载
  onLoad() {
    this.getGoods();
  },

  //加载商品列表分页
  getGoods() {
    const self = this;


    this.data.count++;

    console.log("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK     " + this.data.count)


    let pageData = {
      page_no: this.data.goods_data.page.page_no + 1,
      page_size: this.data.goods_data.page.page_size,
    }


    let data = {
      page: JSON.stringify(pageData),
      service: 'psGoods.smallprogram.freeGetGoodsForOwnListPageFind',
      params: '{}',
      url_type: 'goods',
      app_token: App.globalData.app_token,
      security_code: App.globalData.security_code
    }


    var goods_data = this.data.goods_data
    //限制请求次数，防止商品重复
    if (wx.getStorageSync("getDayUpte") == "") {
      wx.setStorageSync("getDayUpte", "getDayUpte")

    api.reqData({
      data,
      success: (res) => {
        console.log(res.data.data)

        //图片地址由https 改为 http
        var goodlist = res.data.data.list
        for (var i = 0; i < goodlist.length; i++) {
          goodlist[i].pic_info_url = goodlist[i].pic_info_url.replace("https", "http")
          //计算返款金额，保留两位小数（避免0.0098888情况） 映射到参数ts_price上
          goodlist[i].taobao_link = (goodlist[i].taobao_price - goodlist[i].discount_price).toFixed(2);
        }

        goods_data.list = [...goods_data.list, ...goodlist]
        goods_data.page.page_no = res.data.data.page.page_no
        goods_data.page.page_size = res.data.data.page.page_size
        //paginate.total_page = res.data.data.page.total_page
        goods_data.paginate.total_count = res.data.data.page.total_count
        //判断hasNext
        if (goods_data.paginate.total_count > goods_data.page.page_no * goods_data.page.page_size) {
          console.log("后面有数据 还没有查询完")
          goods_data.paginate.hasNext = true
        } else {
          console.log("后面没有数据了，不要再拉了")
          goods_data.paginate.hasNext = false
        }
        this.setData({
          goods_data: goods_data,
        })

        wx.removeStorageSync("getDayUpte")

      }
    })
   }
  },


  toDetail: function (e) {
    //根据taobao_link字段的有无判断是淘宝或精品商品
    //再传与后台值的时候 ds:0 表示精品 ds:1表示淘宝
    let goods_id = e.currentTarget.id;
    wx.navigateTo({
      url: `../detail/selfsupport/selfsupport?goods_id=` + goods_id,
    });
  },



  //拉到底部刷新
  onPullDownRefresh() {
    console.info('onPullDownRefresh')
    if (this.data.goods_data.paginate.hasNext == false) {
      console.log("商品全部查询完毕")
      //拉到底部显示
      this.setData({ loade: false })
    }
    if (!this.data.goods_data.paginate.hasNext) return
    console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
    this.getGoods()
  },

  //商品图片适配
  imageLoad: function (e) {
    var windowWidth = this.data.windowWidth;
    //淘淘领列表查询图片宽高：589 855
    var $width = e.detail.width;
    //var $height = e.detail.height;
    //var ratio = $width / $height; 
    //设置图片显示宽度,适配0.92的比例，
    var viewWidth = windowWidth;
    var viewHeight = viewWidth;    //计算的高度值     
    this.setData({
      imgwidth: viewWidth,
      imgheight: viewHeight
    })
  },
  //返回顶部功能
  scrollTopFun: function (e) {
    if (e.detail.scrollTop > 400) {//触发gotop的显示条件  
      this.setData({
        'goTop_show': true
      });
    } else {
      this.setData({
        'goTop_show': false
      });
    }
  },
  goTopFun: function (e) {
    var _top = this.data.scroll_top;//发现设置scroll-top值不能和上一次的值一样，否则无效，所以这里加了个判断  
    if (_top == 1) {
      _top = 0;
    } else {
      _top = 1;
    }
    this.setData({
      'scroll_top': _top
    });
    console.log("----");
    console.log(this.data.scrollTop)
  }


})